﻿using AutoMapper;
using MediatR;
using S.BreedingHistory.Domain.BreedingHistoryDto;
using S.BreedingHistory.Domain.Model;
using S.BreedingHistory.Infrastructure.Interface;
using S.BreedingHistory.Read.Api.Application.Command;

namespace S.BreedingHistory.Read.Api.Application.CommandHandler
{
    public class BreedFileCardGetByIdCommandHandler : IRequestHandler<BreedFileCardGetByIdCommand, List<FileCardDto>>
    {
        private readonly IBreedingHistoryRepository breedingHistoryRepository;
        private readonly IMapper mapper;

        public BreedFileCardGetByIdCommandHandler(IBreedingHistoryRepository breedingHistoryRepository,IMapper mapper)
        {
            this.breedingHistoryRepository = breedingHistoryRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 通过种母id查询档案
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<List<FileCardDto>> Handle(BreedFileCardGetByIdCommand request, CancellationToken cancellationToken)
        {
            List<BreedingHistoryModel> BreedingHistoryList = await breedingHistoryRepository.GetFileCardById(request.EarTag);
            List<FileCardDto> result = new List<FileCardDto>();
            foreach (var item in BreedingHistoryList)
            {
                FileCardDto fileCardDto=mapper.Map<FileCardDto>(item);
                fileCardDto.Duedate = fileCardDto.BreedingDate.Value.AddDays(120);
                fileCardDto.LiveOffspringNum = fileCardDto.LambNumber + fileCardDto.HealthyNumber + fileCardDto.DeformityNumber;
                result.Add(fileCardDto);
            }
            return result;
        }
    }
}
